//获取应用实例
var app = getApp();
Page({
    data: {
        platform: "iPhone",
        licFeeText: "", /**工本费**/
        serviceFeeText: "", /**服务费**/
        totalFeeText: "", /**总计**/
    },
    onLoad: function () {
        var data = app.getStorageSync(app.globalData.cacheOrderResKey);
        // var databiz = app.getStorageSync(app.globalData.cacheOrderResKey);
        this.setData({

            "licFee": data['licFee'],
            "canUseCoupon": data['canUseCoupon'],
            "discountAmount": data['discountAmount'],
            "docList": data['docList'],
            "serviceFeeDesc": data.serviceFeeDesc,
            "serviceFee": data.serviceFee,
            "totalFee":data.totalFee,
            "cardNbr": data.cardNbr,
            "cardName": data.cardName,
            "certPlaceName": data.certPlaceName,
            "address.street": data['address.street'],
            "address.provinceId": data['address.provinceId'],
            "address.cityId": data['address.cityId'],
            "address.countyId": data['address.countyId'],
            "address.provinceName": data['address.provinceName'],
            "address.cityName": data['address.cityName'],
            "address.countyName": data['address.countyName'],
            "address.cellNum": data['address.cellNum'],
            "address.contactName": data['address.contactName'],
            "address.fullAddr": data['address.fullAddr'],
            "needInvoice": data.needInvoice,
            "certPlaceCityId": data.certPlaceCityId,
            "certPlaceCityName": data.certPlaceCityName,
            "certPlaceProvinceId": data.certPlaceProvinceId,
            "certPlaceProvinceName": data.certPlaceProvinceName,
            "certTypeId": data.certTypeId,
            "certTypeName": data.certTypeName,
        });
        if (app.isAndroid()) {
            this.setData({platform: "android"});
        }
        var licFeeTextTemp = this.getMoneyFormat(this.data.licFee);
        var serviceFeeTextTemp = this.getMoneyFormat(this.data.serviceFee);
        var totalFeeTexttemp = this.getMoneyFormat(this.data.totalFee);
        this.setData({
            licFeeText: licFeeTextTemp,
            serviceFeeText: serviceFeeTextTemp,
            totalFeeText: totalFeeTexttemp,
        });
    },
    getMoneyFormat: function (money) {
      var moneyAtYan = money / 100;
      var moneyResult = "";
      console.log("moneyAtYan:" + moneyAtYan);
      moneyResult = moneyAtYan;
      return moneyResult;
    },
    formSubmit: function (e) {
        var that = this;
        var param = app.getStorageSync(app.globalData.cacheOrderResKey);
        param['platform'] = this.data.platform;
        param['formId'] = e.detail.formId;
        param['payType'] = 3;
        param['accountId'] = app.globalData.openId;
        param['licCityId']=this.data['certPlaceCityId'];
        param['bizType'] = this.data['certTypeId'];
        param['driverName'] = this.data['cardName'];
        param['driverlicNbr']=this.data['cardNbr'];



        var opt = {
            url: app.globalData.host + "/wx/dc/pay.ajax",
            data: param,
            sucFn: function (result) {
                that.pay(result);
                
                //测试更新订单
                // var orderParam = app.getStorageSync(app.globalData.cacheOrderResKey);
                // orderParam["orderId"]=result.body.orderId;
                // orderParam["chagerOrderId"] = result.body.chagerOrderId;
                // app.setStorageSync(app.globalData.cacheOrderResKey, orderParam);
                // that.updateOrder4Success();
                //测试支付完成
                // var orderParam = app.getStorageSync(app.globalData.cacheOrderResKey);
                // orderParam["orderId"] = result.body.orderId;
                // orderParam["chagerOrderId"] = result.body.chagerOrderId;
                // app.setStorageSync(app.globalData.cacheOrderResKey, orderParam);
                // app.redirectTo({url: '../paysuccess/paysuccess'});
            }
        };
        app.ajax(opt);
    },

    pay:function (result) {
       var that = this;
        if (1000 == result.code){
            var payParam = {
                'timeStamp': result.body.wxPayInfo.timeStamp,
                'nonceStr': result.body.wxPayInfo.nonceStr,
                'package': result.body.wxPayInfo.packageName,
                'signType':result.body.wxPayInfo.signType,
                'paySign': result.body.wxPayInfo.paySign,
                sucFn: function (successResult) {
                    console.log("success");
                    console.log(successResult);
                    var orderParam = app.getStorageSync(app.globalData.cacheOrderResKey);
                    orderParam["orderId"] = result.body.orderId;
                    orderParam["chagerOrderId"] = result.body.chagerOrderId;
                    orderParam['prepayId'] = result.body.wxPayInfo.prepayId;
                    orderParam['paySign'] = result.body.wxPayInfo.paySign;
                    orderParam['timeStamp'] = result.body.wxPayInfo.timeStamp;
                    app.setStorageSync(app.globalData.cacheOrderResKey, orderParam);
                    that.updateOrder4Success();
                },
                failFn: function (failResult) {
                   console.log("fail");
                   console.log(failResult);
                },
            };
            app.requestPayment(payParam);
        }else {
            app.errorToast(result.msg);
        }

    },

    //更新订单
    updateOrder4Success: function () {
      var orderParam = app.getStorageSync(app.globalData.cacheOrderResKey);
      var chagerOrderId = orderParam["chagerOrderId"];
      var prePayId = orderParam['prepayId'];
      var moduleOrderId = orderParam['orderId'];
      var platform = this.data.platform;
      var updateOpt = {
        url: app.globalData.host + "/wx/dc/payCallback.ajax",
        data: { 'orderId': chagerOrderId, 'prepayId': prePayId, 'moduleOrderId': moduleOrderId,'platform':platform},
        sucFn: function (result) {
          
        }
      };
      app.ajax(updateOpt);
      var obj = app.getStorageSync(app.globalData.cachePageKey);//下单成功清除身份证号和名称
      obj.cardNbr = null;
      obj.cardName = null;
      obj.num = obj.num + 1;
      app.setStorageSync(app.globalData.cachePageKey, obj);
      app.redirectTo({ url: '../paysuccess/paysuccess' });
    },
});









